package com.example.qimo;

import androidx.appcompat.app.AppCompatActivity;

import android.content.ContentValues;
import android.content.Intent;
import android.database.sqlite.SQLiteDatabase;
import android.nfc.Tag;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import java.text.SimpleDateFormat;
import java.util.Date;

public class show_one_note_detail extends AppCompatActivity {

    private final String Tag="editnote";
public static final String TAG="show_one_note_detail";


     EditText title;
     EditText text;
     TextView last;
     TextView ntime;
     int id1;



    @Override
    protected void onCreate(Bundle savedInstanceState) {


        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_show_one_note_detail);
        title= findViewById(R.id.n_title);
        text = findViewById(R.id.n_text);
        last=findViewById(R.id.lastedit);
        Log.i(Tag,"aaaaa");
        Intent intent=getIntent();
        Bundle bundle=intent.getExtras();

        Log.d(TAG, "onCreate:bundle "+bundle.getString("n_id"));
        String ntitle=intent.getStringExtra("n_title");

        String ntext=intent.getStringExtra("n_text");
        String time=intent.getStringExtra("n_time");
        String id=intent.getStringExtra("n_id");
        id1=Integer.parseInt(id);
        Log.i(Tag,"aaaaa"+ntitle);
        Log.i(Tag,"aaaaa"+ntext);
        Log.i(Tag,"aaaaa"+time);
        Log.i(Tag,"aaaaa"+id);

//        DBhelper mynote = new DBhelper(this);
//        SQLiteDatabase db = mynote.getReadableDatabase();

        title.setText(ntitle);
        text.setText(ntext);
        last.append(time);
        Log.i(Tag,"bbbbb");
//        if(time.getText().length()==0)
//        {
//            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
//            Date date = new Date(System.currentTimeMillis());
//            String str = sdf.format(date);
//            time.setText(str);
//        }

    }



    public void edit(View btn){

        DBhelper mynote=new DBhelper(this);
        String newtitile =title.getText().toString();
        String newtext =text.getText().toString();
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Date date = new Date(System.currentTimeMillis());
        String newtime = sdf.format(date);

        SQLiteDatabase db = mynote.getWritableDatabase();  //数据库读写
        ContentValues values = new ContentValues();
        //数据非空判断
        if("".equals(newtitile)){
            Toast.makeText(show_one_note_detail.this,"标题不能为空",Toast.LENGTH_LONG).show();
            return;
        }
        if("".equals(newtext)) {
            Toast.makeText(show_one_note_detail.this,"内容不能为空",Toast.LENGTH_LONG).show();
            return;
        }
        values.put(DBhelper.TITLE,newtitile);
        values.put(DBhelper.CONTENT,newtext);
        values.put(DBhelper.TIME,newtime);
        values.put(DBhelper.ID,id1);
        db.update(DBhelper.TB_NAME, values, DBhelper.ID + "=?",new String[]{String.valueOf(id1)});
        Toast.makeText(this,"修改成功",Toast.LENGTH_LONG).show();

        Log.i(Tag,"保存时间" + newtime);
        Toast.makeText(show_one_note_detail.this,"保存时间: "+newtime,Toast.LENGTH_LONG).show();


        db.close();
        Intent config = new Intent(show_one_note_detail.this,MainActivity.class);
        config.setFlags(config.FLAG_ACTIVITY_CLEAR_TOP);
        startActivity(config);
        finish();



    }
}